﻿#region File Description
//-----------------------------------------------------------------------------
// HighscoreScreen.cs
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Threading;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using kinect_music_shooter.Screens;
#endregion

namespace kinect_music_shooter
{
    class HighscoreMenuScreen : MenuScreen
    {

        #region Fields

        kinect_music_shooter.Audio.MusicGraph musicGraph;
        MediaLibrary lib = new MediaLibrary();
        Random random = new Random();
        string[] lines;
        int selectedSong = 1;
        MenuEntry lastSelectedMenuEntry;
        MenuEntry songs;
        Texture2D snapshot;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructor.
        /// </summary>
        public HighscoreMenuScreen()
            : base("Highscore")
        {
            musicGraph = new Audio.MusicGraph();

            //StreamReader file = File.OpenText("../../../Highscore.txt");
            //string zeile = file.ReadLine();
            //while (zeile != null && zeile != "")
            //{
            //    string[] spalte = zeile.Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries);
            //    text = spalte[1] + " (" + spalte[0] + ")  -  " + spalte[2];
            //    songs = new MenuEntry(text);
            //    MenuEntries.Add(songs);
            //    zeile = file.ReadLine();
            //}

            lines = System.IO.File.ReadAllLines("Highscore.txt");
            foreach (string zeile in lines)
            {
                string[] spalte = zeile.Split('*');
                songs = new MenuEntry(selectedSong + ") " + spalte[0] + "-" + spalte[1]);
                songs.Selected += fileSelected;
                MenuEntries.Add(songs);
                selectedSong++;
            }

            MenuEntry exitMenuEntry = new MenuEntry("back");
            exitMenuEntry.Selected += OnCancel;
            MenuEntries.Add(exitMenuEntry);
        }

        #endregion

        #region Update and Draw

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            if (getSelectedEntry() != lastSelectedMenuEntry)
            {
                lastSelectedMenuEntry = getSelectedEntry();
                try
                {
                    string[] spalten = getSelectedEntry().Text.Split('-');
                    string title = "";
                    for (int i = 0; i < spalten.Length - 1; i++)
                    {
                        title += spalten[i];
                        if (i < spalten.Length - 2)
                        {
                            title += "-";
                        }
                    }
                    title = title.Split(')')[1].Trim();
                    snapshot = KinectMusicShooterGame.readTextureFromFile(ScreenManager.GraphicsDevice, title + ".png");
                }
                catch (Exception)
                {
                    snapshot = null;
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Rectangle fullscreen = new Rectangle(viewport.Width / 2 - 100, 7, 200, 150); //oben mittig, falls Bild vorhanden

            if (snapshot != null)
            {
                spriteBatch.Begin();
                spriteBatch.Draw(snapshot, fullscreen, new Microsoft.Xna.Framework.Color(TransitionAlpha, TransitionAlpha, TransitionAlpha));
                spriteBatch.End();
            }
        }

        #endregion

        void fileSelected(object sender, PlayerIndexEventArgs e)
        {
            MenuEntry menu = (MenuEntry)sender;
            selectedSong = Convert.ToInt32(menu.Text.Split(')')[0]) - 1;
            LoadingScreen.Load(ScreenManager, false, e.PlayerIndex, new BackgroundScreen(), new DifficultyScreen(lines[selectedSong].Split('*')[2].Trim()));
            KinectMusicShooterGame.musicGraph.setSong(lines[selectedSong].Split('*')[2].Trim());
        }
    }
}
